//
//  QSNewFeatureViewCell.m
//  SinaWeiBo
//
//  Created by apple on 16/6/4.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "QSNewFeatureViewCell.h"

@interface QSNewFeatureViewCell ()
/**
 *  新特性的view
 */
@property (nonatomic, strong) UIImageView *iView;
@end

@implementation QSNewFeatureViewCell

/**
 *  重写initWithFrame方法
 */
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    // 添加背景图片view和新特性的view
    [self.contentView addSubview:self.iView];
    
    return self;
}

#pragma mark - 懒加载
/**
 *  新特性的view
 */
- (UIImageView *)iView
{
    if (_iView == nil) {
        _iView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    }
    
    return _iView;
}

/**
 *  开始体验按钮
 */
- (UIButton *)startButton
{
    // 显示开始体验按钮
    if (_startButton == nil) {
        _startButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
        [_startButton setTitle:@"开始体验" forState:UIControlStateNormal];
        [_startButton.titleLabel setFont:[UIFont systemFontOfSize:18]];
        [_startButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_startButton setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button"] forState:UIControlStateNormal];
        [_startButton setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button_highlighted"] forState:UIControlStateHighlighted];
        
        // 添加按钮点击方法
        [self.startButton addTarget:self action:@selector(clickStartBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _startButton;
}

/**
 *  分享按钮
 */
- (UIButton *)shareButton
{
    // 显示分享按钮
    _shareButton.hidden = NO;
    if (_shareButton == nil) {
        _shareButton = [[UIButton alloc] init];
        [_shareButton setTitle:@"分享新特性" forState:UIControlStateNormal];
        [_shareButton.titleLabel setFont:[UIFont systemFontOfSize:14]];
        [_shareButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [_shareButton setImage:[UIImage imageNamed:@"new_feature_share_true"] forState:UIControlStateNormal];
        
        // 添加按钮点击方法
        [self.shareButton addTarget:self action:@selector(clickshareBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _shareButton;
}

#pragma mark - 按钮点击事件
/**
 *  开始体验按钮点击事件
 */
- (void)clickStartBtn
{
    // 判断代理方法是否存在
    if ([self.delegate respondsToSelector:@selector(startBtnDelegateWithCell:)]) {
        [self.delegate startBtnDelegateWithCell:self];
    }
}

/**
 *  分享按钮点击事件
 */
- (void)clickshareBtn
{
    // 判断代理方法是否存在
    if ([self.delegate respondsToSelector:@selector(shareBtnDelegateWithCell:)]) {
        [self.delegate shareBtnDelegateWithCell:self];
    }
}

/**
 *  动画显示开始体验按钮
 */
- (void)showStartButton
{
    // 显示开始体验按钮
    self.startButton.hidden = NO;
    // 动画方式
    self.startButton.transform = CGAffineTransformMakeScale(0, 0);
    // 用户交互
    self.startButton.userInteractionEnabled = NO;
    
    [UIView animateWithDuration:0.8 delay:0.0 usingSpringWithDamping:0.8 initialSpringVelocity:0 options:UIViewAnimationOptionTransitionNone animations:^{
        self.startButton.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        // 允许用户交互
        self.startButton.userInteractionEnabled = YES;
    }];
}

/**
 *  对按钮进行自动布局
 */
- (void)setupBtnLayout
{
    // 关闭autoResize（因为autoLayout和autoResize不能共存）
    self.startButton.translatesAutoresizingMaskIntoConstraints = NO;
    self.shareButton.translatesAutoresizingMaskIntoConstraints = NO;
    
    // 开始体验
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.startButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.startButton attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-160]];
    
    // 分享按钮
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.shareButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.shareButton attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.startButton attribute:NSLayoutAttributeTop multiplier:1.0 constant:-10]];
}

/**
 *  重写图片索引的set方法
 */
- (void)setImageIndex:(NSInteger)imageIndex
{
    _imageIndex = imageIndex;
    // 拼接图片名
    NSString *imageName = [NSString stringWithFormat:@"new_feature_%ld", (imageIndex + 1)];
    self.iView.image = [UIImage imageNamed:imageName];
}
@end
